package ecweb.ui.gooditem;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecweb.domain.AdminUser;
import ecweb.domain.GoodItem;
import ecweb.exception.DatabaseException;
import ecweb.service.IGoodService;
import ecweb.service.impl.GoodService;

/**
 * Servlet implementation class CreateGoodItemServlet
 */
@WebServlet("/GoodItem/CreateGoodItem")
public class CreateGoodItemServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IGoodService goodService = new GoodService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateGoodItemServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		AdminUser adminuser = (AdminUser) session.getAttribute("admin");
		if (adminuser == null) {
			response.sendRedirect(request.getContextPath() + "/Account/AdminLogin?referrer=" + URLEncoder.encode(request.getContextPath() + "/GoodItem/CreateGoodItem", "UTF-8"));;
			return;
		} else {
			request.getRequestDispatcher("create_gooditem.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession();
		AdminUser adminuser = (AdminUser) session.getAttribute("admin");
		if (adminuser == null) {
			response.sendRedirect(request.getContextPath() + "/Account/AdminLogin");
			return;
		}
		
		String name = request.getParameter("goodname");
		String description = request.getParameter("gooddescription");
		String strPrice = request.getParameter("goodprice");
		String strInventory = request.getParameter("goodinventory");

		if (name == null || "".equals(name) ||
				description == null || "".equals(description)) {
			request.setAttribute("errCode", 1);
			request.getRequestDispatcher("create_gooditem.jsp").forward(request, response);
			return;
		}
		
		double price = 0d;
		int inventory = 0;
		try {
			price = Double.parseDouble(strPrice);
			inventory = Integer.parseInt(strInventory);
		} catch (NumberFormatException e) {
			request.setAttribute("errCode", 2);
			request.getRequestDispatcher("create_gooditem.jsp").forward(request, response);
			return;
		}

		if (price <= 0 || price > 10000000) {
			request.setAttribute("errCode", 3);
			request.getRequestDispatcher("create_gooditem.jsp").forward(request, response);
			return;
		}

		if (inventory <= 0 || inventory > 10000000) {
			request.setAttribute("errCode", 4);
			request.getRequestDispatcher("create_gooditem.jsp").forward(request, response);
			return;
		}

		GoodItem gooditem = null;
		try {
			gooditem = goodService.addGood(name, description, price, inventory);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}

		response.sendRedirect(request.getContextPath() + "/GoodItem/ShowGoodItem?id=" + gooditem.getId());
	}

}
